package org.javaopenaccess.dao.user;

import java.util.List;

import org.javaopenaccess.dao.AbstractDb4oSupportDao;
import org.javaopenaccess.model.User;
import org.javaopenaccess.service.user.UserNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.db4o.query.Predicate;

public class Db4oUserDao extends AbstractDb4oSupportDao implements UserDao {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass().getName());
		
	public Db4oUserDao() {
	}

	@Override
	public User getUser(User user) throws UserNotFoundException {
		//TODO:Not Yet Implemented
		return null;
	}
	@Override
	public void storeUser(User user){
		logger.info("storing user");
		getSession().store(user);

	}
	@SuppressWarnings("serial")
	@Override
	public User getUser(final String userName) throws UserNotFoundException {
		List<User> list = getSession().query(new Predicate<User>() {
			public boolean match(User candidate) {
				return candidate.getUserName().equals(userName);
			}
		});
		if (list.isEmpty())
				throw new UserNotFoundException("No user found");
		return list.get(0);
	}

}
